//
//  ReportView.m
//  Azkar
//
//  Created by 王易 on 2018/8/28.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "ReportView.h"
#import "ReportCell.h"

/**
 举报View
 */
@interface ReportView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,weak) RefreshTableView *tableView;
@end
@implementation ReportView
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initUI];
        
    }
    return self;
    
}
- (void)initUI
{
    self.backgroundColor = [UIColor clearColor];
    
    self.frame = [UIScreen mainScreen].bounds;
    
    MyRelativeLayout *rootLayout = [MyRelativeLayout new];
    rootLayout.myBottom = 0;
    rootLayout.myTop = 0;
    rootLayout.myHorzMargin= 0;
    rootLayout.backgroundColor = [UIColor clearColor];
    [self addSubview:rootLayout];
    
    
    UIView *bgView = [UIView new];
    bgView.myHorzMargin = 0;
    bgView.myBottom = 0;
    bgView.myHeight = [PhoneUtil tabbarAdjustHeight];
    bgView.backgroundColor = [UIColor whiteColor];
    [rootLayout addSubview:bgView];
    
    MyRelativeLayout *contentLayout = [MyRelativeLayout new];
    contentLayout.myHeight = 395;
    contentLayout.myHorzMargin = 0;
    contentLayout.myBottom = [PhoneUtil tabbarAdjustHeight];;
    contentLayout.backgroundColor = [UIColor whiteColor];
    [rootLayout addSubview:contentLayout];
    
    UIButton *topButton = [UIButton buttonWithType:UIButtonTypeCustom];
    topButton.myHorzMargin = topButton.myTop = 0;
    topButton.bottomPos.equalTo(contentLayout.topPos);
    [topButton setHyb_onTouchUp:^(UIButton *sender) {
        [[SKPopupHelper shareInstance] pop_hide];
    }];
    [rootLayout addSubview:topButton];
    
    UIButton *submit = [UIButton buttonWithType:UIButtonTypeCustom];
    [submit changeTitle:TLString(@"Submit")];
    submit.titleLabel.font = FontSize(17);
    [submit changeTitleColor:Color(@"#525252")];
    submit.backgroundColor = Color(@"#F9F9F9");
    submit.myBottom = submit.myHorzMargin = 0;
    submit.myHeight = 56;
    [contentLayout addSubview:submit];
    WeakSelf
    [submit setHyb_onTouchUp:^(UIButton *sender) {
        StrongSelf
        [self submitReport];
    }];
    
    
    RefreshTableView *tableView = [[RefreshTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.myHorzMargin = 0;
    tableView.myTop = 0;
    tableView.bottomPos.equalTo(submit.topPos);
    tableView.estimatedRowHeight = 44;
    tableView.estimatedSectionFooterHeight = 0;
    tableView.estimatedSectionHeaderHeight = 0;
    tableView.backgroundColor = [UIColor whiteColor];
    self.tableView = tableView;
    [contentLayout addSubview:tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [ReportCell registerClassToTable:self.tableView];
    
}
- (void)submitReport
{
    NSMutableArray *report_types = [NSMutableArray array];
    
    [self.reportModel.data enumerateObjectsUsingBlock:^(ReportListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.isSelected) {
            [report_types addObject:obj.ID];
        }
    }];
    if (report_types.count == 0) {
        return;
    }
    
    [AlertTools showLoading];
    [[SKPopupHelper shareInstance] pop_hide];
    NSString *url ;
    if (self.isPublic) {
        url = [NSString stringWithFormat:@"%@/%@/report",PublicBless,self.Id];
    }else{
        url = [NSString stringWithFormat:@"%@/%@/report",BlessBlog,self.Id];
    }
    

    [AlertTools showLoading];
    [[AzkarApi postData:url withParm:@{@"report_types":report_types}] subscribeNext:^(id x) {
        [AlertTools hideLoading];
        [[SKPopupHelper shareInstance] pop_hide];
    } error:^(NSError *error) {
//        [AlertTools hideLoading];
        
    }];
}
- (void)setLang_type:(NSString *)lang_type
{
    _lang_type = lang_type;
    [self loadData];
}
- (void)loadData
{
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/default_report",BlessBlog,self.lang_type];
    [AlertTools showLoading];
    [[AzkarApi getData:url withParm:nil] subscribeNext:^(id x) {
        [AlertTools hideLoading];
        if (x) {
            NSDictionary *data = @{@"data":x};
            ReportModel *model = [ReportModel yy_modelWithDictionary:data];
            self.reportModel = model;
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
//        [AlertTools hideLoading];
        
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.reportModel.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReportCell *cell = [tableView dequeueReusableCellWithIdentifier:[ReportCell reuseIdentifier]];
    cell.reportModel = self.reportModel.data[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReportListModel *m = self.reportModel.data[indexPath.row];
    m.isSelected = !m.isSelected;
    [self.tableView reloadData];
}

- (void)showView
{
    [[SKPopupHelper shareInstance] skpop_FlyInFromBottom:self defaultHideEnable:YES alphaBG:0.3];
}

@end
